home *** CD-ROM | disk | FTP | other *** search
- /************************************************************/
- /* */
- /* MDEF Tester Code from Chapter Three of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1990, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /************************************************************/
-
- #define BASE_RES_ID 400
- #define APPLE_MENU_ID 400
- #define NIL_POINTER 0L
- #define MOVE_TO_FRONT (WindowPtr)-1L
- #define REMOVE_ALL_EVENTS 0
-
- #define WNE_TRAP_NUM 0x60
- #define UNIMPL_TRAP_NUM 0x9F
- #define MIN_SLEEP 60L
- #define NIL_MOUSE_REGION 0L
-
- #define FILE_MENU_ID 401
- #define F_QUIT_ITEM 1
-
- #define PICT_MENU_ID 403
-
-
- Boolean gDone, gWNEImplemented;
- EventRecord gTheEvent;
- MenuHandle gAppleMenu;
- PicHandle gCurPicture;
- WindowPtr gTheWindow;
-
-
- main()
- {
- ToolBoxInit();
- MenuBarInit();
-
- gTheWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT );
- SetPort( gTheWindow );
- ShowWindow( gTheWindow );
-
- gCurPicture = GetPicture( BASE_RES_ID );
-
- MainLoop();
- }
-
-
- /*********************************** ToolBoxInit */
-
- ToolBoxInit()
- {
- InitGraf( &thePort );
- InitFonts();
- FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NIL_POINTER );
- InitCursor();
- }
-
-
- /*********************************** MenuBarInit */
-
- MenuBarInit()
- {
- Handle myMenuBar;
-
- myMenuBar = GetNewMBar( BASE_RES_ID );
- SetMenuBar( myMenuBar );
- gAppleMenu = GetMHandle( APPLE_MENU_ID );
- AddResMenu( gAppleMenu, 'DRVR' );
- DrawMenuBar();
- }
-
-
- /******************************** MainLoop *********/
-
- MainLoop()
- {
- gDone = FALSE;
- gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
- NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
- while ( gDone == FALSE )
- {
- HandleEvent();
- }
- }
-
-
- /************************************* HandleEvent */
-
- HandleEvent()
- {
- char theChar;
-
- if ( gWNEImplemented )
- WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, NIL_MOUSE_REGION );
- else
- {
- SystemTask();
- GetNextEvent( everyEvent, &gTheEvent );
- }
-
- switch ( gTheEvent.what )
- {
- case mouseDown:
- HandleMouseDown();
- break;
- case keyDown:
- case autoKey:
- theChar = gTheEvent.message & charCodeMask;
- if (( gTheEvent.modifiers & cmdKey ) != 0)
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case updateEvt:
- BeginUpdate( (WindowPtr)gTheEvent.message );
- DrawMyPicture( gCurPicture, gTheWindow );
- EndUpdate( (WindowPtr)gTheEvent.message );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- HandleMouseDown()
- {
- WindowPtr whichWindow;
- short int thePart;
- long int menuChoice, windSize;
-
- thePart = FindWindow( gTheEvent.where, &whichWindow );
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( gTheEvent.where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( &gTheEvent, whichWindow );
- break;
- case inDrag :
- DragWindow( whichWindow, gTheEvent.where, &(screenBits.bounds) );
- break;
- }
- }
-
-
- /************************************* HandleMenuChoice */
-
- HandleMenuChoice( menuChoice )
- long int menuChoice;
- {
- int theMenu;
- int theItem;
-
- if ( menuChoice != 0 )
- {
- theMenu = HiWord( menuChoice );
- theItem = LoWord( menuChoice );
- switch ( theMenu )
- {
- case FILE_MENU_ID :
- if ( theItem == F_QUIT_ITEM )
- gDone = TRUE;
- break;
- case PICT_MENU_ID :
- EraseRect( &gTheWindow->portRect );
- InvalRect( &gTheWindow->portRect );
- gCurPicture = GetPicture( BASE_RES_ID + theItem - 1 );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /******************************** DrawMyPicture *********/
-
- DrawMyPicture( thePicture, pictureWindow )
- PicHandle thePicture;
- WindowPtr pictureWindow;
- {
- Rect myRect;
-
- myRect = pictureWindow->portRect;
- CenterPict( thePicture, &myRect );
- DrawPicture( thePicture, &myRect );
- }
-
-
- /******************************** CenterPict *********/
-
- CenterPict( thePicture, myRectPtr )
- PicHandle thePicture;
- Rect *myRectPtr;
- {
- Rect windRect, pictureRect;
-
- windRect = *myRectPtr;
- pictureRect = (**( thePicture )).picFrame;
- myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
- / 2 + windRect.top;
- myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
- myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
- / 2 + windRect.left;
- myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
- }